package com.study.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.github.pagehelper.PageInfo;
import com.study.model.Echarts;
import com.study.model.EchartsRoleUser;
import com.study.model.Logs;
import com.study.model.logPOi;
import com.study.service.EchartService;
import com.study.service.LogService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author king james
 * @create 2021-09-03-{TIME}
 */
@RestController
@RequestMapping("/logs")
public class LogController {
    @Autowired
    private LogService logService;

    @Autowired
    private EchartService echartService;


//    @RequestMapping("/queryLog")
//    public void queryAllLogInfo(){
//      for (Logs logs:logService.queryAllLogInfo()){
//          System.out.println(logs.getLogDesc() + "哈哈哈");
//      }
//    }
@RequestMapping
/**
 * 查询所有日志记录信息
 */
public Map<String,Object> getAll(Logs logs, String draw,
                                 @RequestParam(required = false, defaultValue = "1") int start,
                                 @RequestParam(required = false, defaultValue = "10") int length){

            Map<String,Object> map = new HashMap<>();
//            System.out.println("测试我的日志输入"  + logs.getEndTime());
            PageInfo<Logs> pageInfo = logService.selectByPage(logs,start, length);
            map.put("draw",draw);
            map.put("recordsTotal",pageInfo.getTotal());
            map.put("recordsFiltered",pageInfo.getTotal());
            map.put("data", pageInfo.getList());
            return map;
            }

    /**
     * 导出日志信息
     * @return
     */
    @RequestMapping(value = "exportLog")
            public String exportLog() throws IOException {
              List<logPOi> logList =  logService.exportLog();

        //设置Excel的描述文件
        ExportParams exportParams = new ExportParams("日志列表的所有数据", "日志信息" , ExcelType.XSSF);
        //进行导出的基本操作
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, logPOi.class, logList);
        //输入输出流地址
        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\logs.xlsx");
        //进行输出流
        workbook.write(fileOutputStream);
        //关流
        fileOutputStream.close();
        workbook.close();
        System.out.println(logList.size() + "长度");
        if (logList!=null){
            return "success";
        }
        else {
            return "fail";
        }
            }
    /**
     * 新增日志信息
     * @param logs 日志信息
     * @return
     */
    @RequestMapping(value = "/add")
    public String addLog(Logs logs){
//        System.out.println("测试我的日志" + logs);
        if (logService.addLog(logs.getLogDesc())>0){
            return "success";
        }else {
            return "fail";
        }
    }
    //删除日志(根据id)
    @RequestMapping(value = "delete")
    public  String deleteLog(String ids){
        /*对前端传入的字符串处理*/
        String[] a = ids.split(",");
        int array[] = new int[a.length];
        for(int i=0;i<a.length;i++) {
            array[i] = Integer.parseInt(a[i]);
        }
//        for (Integer id:array){
//            logService.deleteLog(id);
//        }
        logService.deleteLog(array);
        return "success";
    }

    /**
     * 各个模块数量统计
     * @return
     */
    @RequestMapping(value = "echartsList")
    public Echarts echartsList(){
        Echarts echarts = new Echarts();
        int userCount = echartService.queryUserCount();
        int roleCount = echartService.queryRoleCount();
        int resourcesCount = echartService.queryResourcesCount();
        int logCount = echartService.queryLogCount();
        /*获取到相应的比例*/
        echarts.setUserCount(userCount);
        echarts.setRoleCount(roleCount);
        echarts.setResourcesCount(resourcesCount);
        echarts.setLogCount(logCount);
        return echarts;
    }

    /**
     * 统计各个角色的用户数量
     * @return
     */
    @RequestMapping(value = "/roleUser")
    public EchartsRoleUser roleUser(){
        return echartService.roleUserCount();
    }


}
